package com.cenwangkeji.resident.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.bean.SuccessBean;
import com.cenwangkeji.resident.presenter.activity.AddQuestionPresenter;
import com.cenwangkeji.resident.ui.adapter.QuestionimageAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
//添加问题
public class AddQuestionActivity extends BaseActivity implements AddQuestionPresenter.I,BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.et_title)
    EditText mEtTitle;
    @BindView(R.id.et_content)
    EditText mEtContent;
    @BindView(R.id.rv)
    RecyclerView mRv;
    private  List<String> mPath = new ArrayList<>();
    private QuestionimageAdapter mAdapter;
    private AddQuestionPresenter mPresenter = new AddQuestionPresenter(this);
    @Override
    public int setLayoutId() {
        return R.layout.activity_add_question;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        initRecycelrView();
    }

    @OnClick({R.id.iv_add,R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()){
            case R.id.iv_add:
                openPhoto();
                break;
            case R.id.btn:
                add();
                break;
        }
    }

    //添加问题
    private void add(){
        String title = mEtTitle.getText().toString();
        String content = mEtContent.getText().toString();
        if (TextUtils.isEmpty(title)){
            MyToast.show("标题不能为空");
        }else if (TextUtils.isEmpty(content)){
            MyToast.show("内容不能为空");
        }else {
            showLoading();
            mPresenter.add(title,content,(List<String>)mAdapter.getData());
        }
    }

    private void initRecycelrView(){
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRv.setLayoutManager(manager);
        mAdapter = new QuestionimageAdapter(mPath);
        mAdapter.setOnItemChildClickListener(this);
        mRv.setAdapter(mAdapter);
    }

    //打开相册
    public void openPhoto() {
        PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .minSelectNum(1)
                .maxSelectNum(6)
                .selectionMode(PictureConfig.MULTIPLE )
                .previewImage(true)
                .isCamera(true)
                .compress(true)
                .enableCrop(false)
                .forResult(PictureConfig.CHOOSE_REQUEST);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    final List<LocalMedia> list = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia localMedia:list){
                        mPath.add(localMedia.getCompressPath());
                        MyLogger.i(localMedia.getCompressPath());
                    }
                    mAdapter.replaceData(mPath);


                  //  Glide.with(this).load(list.get(0).getCompressPath()).apply(new RequestOptions()//          .error(R.mipmap.ic_task_add)).into(mIv);
                 //   mFilePath = list.get(0).getCompressPath();

            }
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                mAdapter.remove(position);
    }

    @Override
    public void onAddSuccess(SuccessBean bean) {
        dismissLoading();
        if (bean.getCode()==0){
            MyToast.show("操作成功");
            finish();
        }else {
            MyToast.show(bean.getMsg());
        }
    }
}
